import fs from "fs";
import path from "path";

export const copyDir = (from: string, to: string) => {
  const list = fs.readdirSync(from);
  list.forEach((el: string) => {
    const subFrom = path.join(from, el);
    const subTo = path.join(to, el);
    if (fs.statSync(subFrom).isDirectory()) {
      // if (!fs.existsSync(subFrom)) fs.mkdirSync(subTo);
      try {
        fs.mkdirSync(subTo);
      } catch (err) {
        console.log(err);
      }
      copyDir(subFrom, subTo);
    } else {
      fs.copyFileSync(subFrom, subTo);
    }
  });
};

export const delDir = (dir: string) => {
  if (!fs.existsSync(dir)) {
    console.log(dir);
    throw new Error("dir does not exist!");
  }
  const list = fs.readdirSync(dir);
  list.forEach((el: string) => {
    const subDir = path.join(dir, el);
    if (fs.statSync(subDir).isDirectory()) {
      delDir(subDir);
    } else {
      fs.unlinkSync(subDir);
    }
  });
  fs.rmdirSync(dir);
};
